<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.model.Book" %>
<%@ page import="com.example.model.CartItem" %>
<%
    // 登录校验，未登录跳转登录页
    if (session.getAttribute("user") == null) {
        response.sendRedirect("login.jsp");
        return;
    }
    List<CartItem> cartItems = (List<CartItem>) request.getAttribute("cartItems");
    Double cartTotal = (Double) request.getAttribute("cartTotal");
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的购物车 - 应急防护小站</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-orange-50 min-h-screen text-gray-900">
    <!-- 顶部导航栏 -->
    <nav class="bg-white shadow-sm sticky top-0 z-40">
        <div class="max-w-7xl mx-auto flex items-center justify-between px-4 h-16">
            <div class="flex items-center space-x-2">
                <i class="fa-solid fa-shield-alt text-green-600 text-2xl"></i>
                <span class="text-xl font-bold">应急防护小站</span>
            </div>
            <div class="flex items-center space-x-2">
                <a href="index.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("index.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-house mr-1"></i>首页</a>
                <a href="quiz.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("quiz.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-question-circle mr-1"></i>答题功能</a>
                <a href="/books" class="px-4 py-2<%=(request.getRequestURI().endsWith("books.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-cart-shopping mr-1"></i>图书购买</a>
                <a href="user-profile.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("user-profile.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-user mr-1"></i>个人信息</a>
                <a href="submit-article.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("submit-article.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-pen-to-square mr-1"></i>我要投稿</a>
                <a href="feedback.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("feedback.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-comment-dots mr-1"></i>问题反馈</a>
            </div>
        </div>
    </nav>
    <main class="max-w-5xl mx-auto px-4 py-12">
        <h2 class="text-2xl font-bold mb-8 flex items-center text-orange-700">
            <i class="fa-solid fa-cart-shopping text-orange-600 mr-3"></i> 我的购物车
        </h2>
        <% if (cartItems == null || cartItems.isEmpty()) { %>
            <div class="bg-white rounded-xl shadow p-12 text-center text-gray-400">
                <i class="fa-solid fa-cart-shopping text-6xl mb-4"></i>
                <div class="text-lg mb-2">您的购物车是空的</div>
                <a href="books" class="inline-block mt-4 bg-orange-600 hover:bg-orange-700 text-white px-6 py-3 rounded-lg font-bold">去选购图书</a>
            </div>
        <% } else { %>
            <form action="cart" method="post">
                <table class="w-full text-left mb-8">
                    <thead>
                        <tr class="bg-orange-100">
                            <th class="py-3 px-2">封面</th>
                            <th>书名</th>
                            <th>单价</th>
                            <th>数量</th>
                            <th>小计</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <% for (CartItem item : cartItems) { Book book = item.getBook(); %>
                        <tr class="border-b">
                            <td class="py-2"><img src="<%= book.getCoverUrl() %>" class="h-16 w-12 object-cover rounded"/></td>
                            <td><a href="/book-detail?bookId=<%= book.getId() %>" class="text-orange-700 hover:underline"><%= book.getName() %></a></td>
                            <td>￥<%= book.getPrice() %></td>
                            <td>
                                <input type="number" name="quantity_<%= book.getId() %>" value="<%= item.getQuantity() %>" min="1" max="<%= book.getStock() %>" class="w-16 px-2 py-1 border border-gray-300 rounded">
                            </td>
                            <td>￥<%= String.format("%.2f", book.getPrice() * item.getQuantity()) %></td>
                            <td>
                                <button name="action" value="remove_<%= book.getId() %>" class="text-red-600 hover:underline" type="submit">删除</button>
                            </td>
                        </tr>
                        <% } %>
                    </tbody>
                </table>
                <div class="flex justify-between items-center">
                    <div>
                        <button name="action" value="update" class="bg-orange-500 hover:bg-orange-600 text-white px-6 py-2 rounded-lg font-bold mr-4">更新数量</button>
                        <button name="action" value="clear" class="bg-gray-300 hover:bg-gray-400 text-gray-700 px-6 py-2 rounded-lg font-bold">清空购物车</button>
                    </div>
                    <div class="text-xl font-bold">总价：<span class="text-orange-600">￥<%= cartTotal != null ? String.format("%.2f", cartTotal) : "0.00" %></span></div>
                    <button name="action" value="checkout" class="bg-green-600 hover:bg-green-700 text-white px-8 py-3 rounded-lg font-bold text-lg">结账</button>
                </div>
            </form>
        <% } %>
    </main>
    <!-- 底部信息区 -->
    <footer class="bg-gray-900 text-white py-10 mt-16">
        <div class="max-w-7xl mx-auto px-4">
            <div class="grid grid-cols-1 md:grid-cols-4 gap-8">
                <div>
                    <div class="flex items-center space-x-2 mb-4">
                        <i class="fa-solid fa-shield-alt text-orange-400 text-2xl"></i>
                        <span class="text-xl font-bold">应急防护小站</span>
                    </div>
                    <p class="text-gray-400 text-sm mb-4">守护生命安全</p>
                    <div class="flex space-x-4">
                        <a href="#" class="text-gray-400 hover:text-white"><i class="fa-brands fa-weibo"></i></a>
                        <a href="#" class="text-gray-400 hover:text-white"><i class="fa-brands fa-weixin"></i></a>
                        <a href="#" class="text-gray-400 hover:text-white"><i class="fa-brands fa-twitter"></i></a>
                    </div>
                </div>
                <div>
                    <h3 class="text-lg font-medium mb-4">防护分类</h3>
                    <ul class="space-y-2 text-gray-400">
                        <li><a href="disaster-earthquake.jsp" class="hover:text-white">地震防护</a></li>
                        <li><a href="disaster-fire.jsp" class="hover:text-white">火灾防护</a></li>
                        <li><a href="disaster-typhoon.jsp" class="hover:text-white">台风防护</a></li>
                        <li><a href="disaster-flood.jsp" class="hover:text-white">洪水防护</a></li>
                        <li><a href="disaster-extremeheat.jsp" class="hover:text-white">极端天气</a></li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-lg font-medium mb-4">快速链接</h3>
                    <ul class="space-y-2 text-gray-400">
                        <li><a href="#about" class="hover:text-white">关于我们</a></li>
                        <li><a href="submit-article.jsp" class="hover:text-white">我要投稿</a></li>
                        <li><a href="books" class="hover:text-white">防护用品</a></li>
                        <li><a href="feedback.jsp" class="hover:text-white">问题反馈</a></li>
                        <li><a href="#" class="hover:text-white">隐私政策</a></li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-lg font-medium mb-4">订阅我们</h3>
                    <p class="text-gray-400 text-sm mb-4">订阅我们的防护周刊，获取最新应急资讯</p>
                    <div class="flex">
                        <input type="email" placeholder="输入您的邮箱" class="px-4 py-2 rounded-l-lg w-full focus:outline-none focus:ring-2 focus:ring-orange-400">
                        <button class="bg-orange-600 hover:bg-orange-700 text-white px-4 rounded-r-lg"><i class="fa-solid fa-paper-plane"></i></button>
                    </div>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-500 text-sm">
                <p>&copy; 2025 应急防护小站. 保留所有权利.</p>
            </div>
        </div>
    </footer>
    <script>
        // 登录判断，未登录跳转到登录页
        function checkLogin(url) {
            var isLogin = '<%= session.getAttribute("user") != null ? "true" : "false" %>';
            if (isLogin === 'false') {
                window.location.href = 'login.jsp';
            } else {
                window.location.href = url;
            }
            return false;
        }
    </script>
</body>
</html> 